#ifndef __REC_INDEX_H__
#define __REC_INDEX_H__

#include "avi.h"

#define MAV_FRAME_ISLICE 1
#define MAV_FRAME_PSLICE 2
#define MAV_FRAME_ASLICE 3
struct mav_frame_header
{/* 24 bytes */
    unsigned int type; // video:I frame,P frame,audio:Audio frame
    unsigned int len;
    unsigned long long pts;
    time_t fts;// frame timestamp 
};

typedef struct mav_index_item
{
    unsigned int index;
    time_t timestamp;
    long offset;
    unsigned int type;
}st_mav_index_item;

//typedef unsigned int FOURCC;
//#define TODWORD(ch) ((unsigned int)(unsigned char)(ch))
//#define TOWORD(ch) ((unsigned short)(unsigned char)(ch))
//#define mkFOURCC(ch0,ch1,ch2,ch3) (TODWORD(ch0)|TODWORD(ch1)<<8|TODWORD(ch2)<<16|TODWORD(ch3)<<24)
struct mav_header_list
{/* 88 bytes */
    FOURCC mavh;
    long index_offset;
    unsigned int index_interval; // default:1s,min: 1s, only record I frame index.
    unsigned int index_num;
    st_mav_index_item index_begin;
    st_mav_index_item index_end;
};
#define MAV_INDEX_MAGIC (0x234d9e7b)
struct mav_index_list 
{
    unsigned int magic;
    unsigned int sum;
    st_mav_index_item item[];
};

enum mav_stream_type
{
    MAV_STREAM_VIDEO = 0,
    MAV_STREAM_AUDIO,
    MAV_STREAM_VAMIX,
    MAV_STREAM_META,
    MAV_STREAM_MAX,
};
#define MAV_INITIAL_INDEX_NUM 100
typedef struct mav_handle
{
    char fname[64];
    FILE *fp;
    unsigned int index_max; // current max index available
    struct mav_header_list header;
    struct mav_index_list *index;
}*MAV_HANDLE;


MAV_HANDLE mav_init_handle(const char *filename,int strtype);
int mav_write_packet(MAV_HANDLE pmav,int type,unsigned char *buf,int size);
void mav_exit_handle(MAV_HANDLE pmav);

int mav_fix_handle(const char *filename);
int mav_get_tszone(const char *filename,time_t *pstart,time_t *pend);
int mav_get_current_tszone(time_t *pstart,time_t *pend);
FILE* mav_seek_frame(const char *filename,time_t tnode);
FILE* mav_seek_current(time_t tstart,time_t *pstart,time_t *ptend);
int mav_read_packet(FILE *fp,struct mav_frame_header *header,unsigned char *frame,int len);
int mav_read_prev_packet(FILE *fp,struct mav_frame_header *header,unsigned char *frame,int len);

#endif
